﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using AdoDAL.Repositories;
using Stiline.Web.Models.Account;
//using Stiline.DAL.Repositories;

namespace Stiline.Web.Controllers
{
    public class AccountController : Controller
    {
        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        public ActionResult Login(LoginModel loginModel)
        {
            if (this.IsValidLoginArgument(loginModel.Login, loginModel.Password))
            {
                try
                {
                    if (Membership.ValidateUser(loginModel.Login, loginModel.Password))
                    {
                        return RedirectFromLoginPage(loginModel.Login, loginModel.ReturnUrl);
                    }
                    else
                    {
                        ViewBag.Error = true;
                        ViewBag.ErrorMessage =
                            "Login failed! Make sure you have entered the right user name and password!";
                    }
                }
                catch (SqlException e)
                {
                    ViewBag.Error = true;
                    ViewBag.ErrorMessage = "Something wrong with server\n" + e.Message;
                }
            }
            return View();
        }

        public ActionResult Logout()
        {
            Session.Clear();
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }

        private ActionResult RedirectFromLoginPage(string userName, string returnUrl)
        {
            FormsAuthentication.SetAuthCookie(userName, false);

            if (!string.IsNullOrEmpty(returnUrl))
            {
                return Redirect(returnUrl);
            }
            return RedirectToAction("Index", "Home");
        }

        private bool IsValidLoginArgument(string userName, string password)
        {
            return !(string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password));
        }

        [HttpGet]
        [AllowAnonymous]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        public ActionResult Create(UserModel userModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var userRepository = new UserRepository();
                    var userExist =
                        userRepository.GetAll()
                                      .Any(u => u.Name.Equals(userModel.Name) || u.Email.Equals(userModel.Email));
                    if (userExist)
                    {
                        return View();
                    }
                    var newUser = Membership.CreateUser(userModel.Name, userModel.Password, userModel.Email);
                    return View("CreateSuccess");
                }
                catch (SqlException e)
                {
                    ViewBag.Error = true;
                    ViewBag.ErrorMessage = "Something wrong with server\n" + e.Message;
                }
            }
            return View();
        }
    }
}
